package com.xiaoyi.ui.fragment.pic;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.SingleObserver;
import io.reactivex.disposables.Disposable;
import me.yokeyword.eventbusactivityscope.EventBusActivityScope;
import me.yokeyword.fragmentation.SupportFragment;
import com.avin.yi.R;
import com.xiaoyi.adapter.XyPicTypeListAdapter;
import com.xiaoyi.event.TabSelectedEvent;
import com.xiaoyi.tool.AvBaseTool;
import com.xiaoyi.tool.http.XyApi;
import com.xiaoyi.ui.fragment.base.BaseMainFragment;
import com.yanzhenjie.recyclerview.swipe.SwipeItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;


public class PicTypeFragment extends BaseMainFragment {
    private SwipeMenuRecyclerView recyclerview;
    private SwipeRefreshLayout mRefreshLayout;
    private FloatingActionButton mFab;
    private int mScrollTotal;
    private XyPicTypeListAdapter mAdapter;

    public static PicTypeFragment newInstance() {
        Bundle args = new Bundle();
        PicTypeFragment fragment = new PicTypeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fram_pictype, container, false);
        EventBusActivityScope.getDefault(_mActivity).register(this);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mFab =  view.findViewById(R.id.fab);
        mFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                recyclerview.smoothScrollToPosition(0);
            }
        });

        mRefreshLayout =  view.findViewById(R.id.mRefreshLayout);
        mRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
        mRefreshLayout.setOnRefreshListener(mRefreshListener);

        recyclerview =  view.findViewById(R.id.recyclerview);
        recyclerview.setLayoutManager(new GridLayoutManager(_mActivity, 4));
        recyclerview.setSwipeItemClickListener(swipeItemClickListener);
        recyclerview.addOnScrollListener(onScrollListener  );
        mAdapter = new XyPicTypeListAdapter(_mActivity);
        recyclerview.setAdapter(mAdapter);

        loadData(  );
    }




    /**
     * 选择tab事件 . eventbus 需要，用来处理 双击 tab 事件
     */
    @Subscribe
    public void onTabSelectedEvent(TabSelectedEvent event) {
        AvBaseTool.showMsg(_mActivity ,"onTabSelectedEvent="+ event.position);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBusActivityScope.getDefault(_mActivity).unregister(this);
    }

    private SwipeItemClickListener swipeItemClickListener = new SwipeItemClickListener() {
        @Override
        public void onItemClick(View itemView, int position) {
            /// AvBaseTool.showMsg(_mActivity  ,"---"+position);
            PicDetailFragment fragment = PicDetailFragment.newInstance(mAdapter.getItem(position));
            EventBusActivityScope.getDefault(_mActivity).post( fragment );
        }
    };
    private void loadData(  ) {
        showLoadDialog2(_mActivity);
        XyApi.getInstance(_mActivity).getDataDirByType(new SingleObserver<ArrayList<String>>() {
            @Override
            public void onSubscribe(Disposable d) {    }

            @Override
            public void onSuccess(ArrayList<String> list) {
                hideLoadDialog2();
                mRefreshLayout.setRefreshing(false);
                updateData(list );

            }

            @Override
            public void onError(Throwable e) {
                hideLoadDialog2();
                mRefreshLayout.setRefreshing(false);
                AvBaseTool.showMsg(_mActivity ,"错误："+ e.getMessage());
            }
        }  , "图片");

    }


    private List<String> mDataList = new ArrayList<>();
    private void updateData(ArrayList<String>  result) {
        mDataList.clear();
        mDataList = result;
        mAdapter.notifyDataSetChanged(mDataList);
        recyclerview.loadMoreFinish(false, true);
    }



    private SwipeRefreshLayout.OnRefreshListener mRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            loadData(  );
        }
    };


    private RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            mScrollTotal += dy;
            if (mScrollTotal <= 0) {
                mFab.hide();
            } else {
                mFab.show();
            }
        }
    } ;


}
